package com.cypher.encryptionanddecryption.fragment.protocol.nb25;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.cypher.encryptionanddecryption.R;
import com.cypher.encryptionanddecryption.activity.BluetoothCommunicationActivity;
import com.cypher.encryptionanddecryption.bean.CommunicationBean;
import com.cypher.encryptionanddecryption.fragment.protocol.AbstractProtocolFragment;
import com.cypher.encryptionanddecryption.util.HexUtils;
import com.cypher.encryptionanddecryption.util.Nb25ProtocolUtils;

import org.apache.commons.lang3.StringUtils;

/**
 * 表状态页面
 * @author: 古霸天
 * @date: 2020/11/23
 */
public class MeterStateFragment extends AbstractProtocolFragment {

    private final String[] onOrOffArr = {"关", "开"};
    private final String[] trueOrFalseArr = {"否", "是"};

    private TextView meterStateTxt;
    /**
     * 阀门状态
     */
    private Spinner valveState;
    /**
     * 表具被强制命令开阀
     */
    private Spinner compulsiveOpenValve;
    /**
     * 主电电量低
     */
    private Spinner mainChargeLow;
    /**
     * 备电电量不足
     */
    private Spinner standbyPowerLow;
    /**
     * 无备电，系统不能正常工作
     */
    private Spinner noElectricity;
    /**
     * 异常大流量
     */
    private Spinner abnormallyHigh;
    /**
     * 阀门直通
     */
    private Spinner valveDirect;
    /**
     * 外部报警触发
     */
    private Spinner externalAlarmTrigger;

    /**
     * 计量模块异常
     */
    private Spinner abnormalMeasurementModule;
    /**
     * 多天不用气导致阀门关闭
     */
    private Spinner noGasValveClose;
    /**
     * 电磁干扰
     */
    private Spinner electromagneticInterference;
    /**
     * 外壳拆卸告警
     */
    private Spinner shellRemovalAlarm;
    /**
     * 曾出现多天没有远传数据上传成功而导致阀门关闭
     */
    private Spinner noDataValveClose;

    private CommunicationBean bean;
    private BluetoothCommunicationActivity activity;
    public MeterStateFragment(BluetoothCommunicationActivity activity, CommunicationBean bean) {
        this.activity = activity;
        this.bean = bean;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_nb25_meter_state, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        init(view);
    }

    /**
     * 初始化
     * @param v
     */
    private void init(View v) {
        ArrayAdapter onOrOffAdapter = new ArrayAdapter<String>(v.getContext(), R.layout.spinner_item, onOrOffArr);
        onOrOffAdapter.setDropDownViewResource(R.layout.dropdown_stytle);

        ArrayAdapter trueOrFalseAdapter = new ArrayAdapter<String>(v.getContext(), R.layout.spinner_item, trueOrFalseArr);
        trueOrFalseAdapter.setDropDownViewResource(R.layout.dropdown_stytle);

        meterStateTxt = v.findViewById(R.id.meter_state_txt);

        valveState = v.findViewById(R.id.spinner_valve_state);
        valveState.setAdapter(onOrOffAdapter);
        compulsiveOpenValve = v.findViewById(R.id.spinner_compulsive_open_valve);
        compulsiveOpenValve.setAdapter(trueOrFalseAdapter);
        mainChargeLow = v.findViewById(R.id.spinner_main_charge_low);
        mainChargeLow.setAdapter(trueOrFalseAdapter);
        standbyPowerLow = v.findViewById(R.id.spinner_standby_power_low);
        standbyPowerLow.setAdapter(trueOrFalseAdapter);
        noElectricity = v.findViewById(R.id.spinner_no_electricity);
        noElectricity.setAdapter(trueOrFalseAdapter);
        abnormallyHigh = v.findViewById(R.id.spinner_abnormally_high);
        abnormallyHigh.setAdapter(onOrOffAdapter);
        valveDirect = v.findViewById(R.id.spinner_valve_direct);
        valveDirect.setAdapter(onOrOffAdapter);
        externalAlarmTrigger = v.findViewById(R.id.spinner_external_alarm_trigger);
        externalAlarmTrigger.setAdapter(onOrOffAdapter);

        abnormalMeasurementModule = v.findViewById(R.id.spinner_abnormal_measurement_module);
        abnormalMeasurementModule.setAdapter(trueOrFalseAdapter);
        noGasValveClose = v.findViewById(R.id.spinner_no_gas_valve_close);
        noGasValveClose.setAdapter(trueOrFalseAdapter);
        electromagneticInterference = v.findViewById(R.id.spinner_electromagnetic_interference);
        electromagneticInterference.setAdapter(trueOrFalseAdapter);
        shellRemovalAlarm = v.findViewById(R.id.spinner_shell_removal_alarm);
        shellRemovalAlarm.setAdapter(trueOrFalseAdapter);
        noDataValveClose = v.findViewById(R.id.spinner_no_data_valve_close);
        noDataValveClose.setAdapter(trueOrFalseAdapter);

    }

    @Override
    public void fillFragment(String data) {
        reader(Nb25ProtocolUtils.formatReceiveData(data));
    }
    @Override
    public String getHexCode(String rwType) {
        if (StringUtils.equals("read", rwType)) {
            return Nb25ProtocolUtils.getReadCommand(bean.getHexCode(), PROTOCOL_VERSION);
        }
        // 组包
        return Nb25ProtocolUtils.getWriteCommand(bean.getHexCode(), getData(), PROTOCOL_VERSION);
    }

    /**
     * 渲染页面
     * @param data
     */
    private void reader(String data) {
        meterStateTxt.setText(data);

        String binary1 = HexUtils.paddingLeft(Integer.toBinaryString(Integer.parseInt(data.substring(0, 2), 16)), 8);
        int start1 = 0;
        externalAlarmTrigger.setSelection(Integer.parseInt(getBinary(binary1, start1)));
        start1 += 1;
        valveDirect.setSelection(Integer.parseInt(getBinary(binary1, start1)));
        start1 += 1;
        abnormallyHigh.setSelection(Integer.parseInt(getBinary(binary1, start1)));
        start1 += 1;
        noElectricity.setSelection(Integer.parseInt(getBinary(binary1, start1)));
        start1 += 1;
        standbyPowerLow.setSelection(Integer.parseInt(getBinary(binary1, start1)));
        start1 += 1;
        mainChargeLow.setSelection(Integer.parseInt(getBinary(binary1, start1)));
        start1 += 1;
        compulsiveOpenValve.setSelection(Integer.parseInt(getBinary(binary1, start1)));
        start1 += 1;
        valveState.setSelection(Integer.parseInt(getBinary(binary1, start1)));

        String binary2 = HexUtils.paddingLeft(Integer.toBinaryString(Integer.parseInt(data.substring(2, 4), 16)), 8);
        int start2 = 3;
        shellRemovalAlarm.setSelection(Integer.parseInt(getBinary(binary2, start2)));
        start2 += 1;
        electromagneticInterference.setSelection(Integer.parseInt(getBinary(binary2, start2)));
        start2 += 1;
        noDataValveClose.setSelection(Integer.parseInt(getBinary(binary2, start2)));
        start2 += 1;
        noGasValveClose.setSelection(Integer.parseInt(getBinary(binary2, start2)));
        start2 += 1;
        abnormalMeasurementModule.setSelection(Integer.parseInt(getBinary(binary2, start2)));
    }
    private String getBinary(String binary, int start) {
        return binary.substring(start, start + 1);
    }
    /**
     * 获取数据域
     * @return
     */
    private String getData() {
        StringBuffer state1 = new StringBuffer();
        state1.append(externalAlarmTrigger.getSelectedItemPosition());
        state1.append(valveDirect.getSelectedItemPosition());
        state1.append(abnormallyHigh.getSelectedItemPosition());
        state1.append(noElectricity.getSelectedItemPosition());
        state1.append(standbyPowerLow.getSelectedItemPosition());
        state1.append(mainChargeLow.getSelectedItemPosition());
        state1.append(compulsiveOpenValve.getSelectedItemPosition());
        state1.append(valveState.getSelectedItemPosition());

        StringBuffer state2 = new StringBuffer("000");
        state2.append(shellRemovalAlarm.getSelectedItemPosition());
        state2.append(electromagneticInterference.getSelectedItemPosition());
        state2.append(noDataValveClose.getSelectedItemPosition());
        state2.append(noGasValveClose.getSelectedItemPosition());
        state2.append(abnormalMeasurementModule.getSelectedItemPosition());

        String data1 = HexUtils.paddingLeft(Integer.toHexString(Integer.parseInt(state1.toString(), 2)), 2);
        String data2 = HexUtils.paddingLeft(Integer.toHexString(Integer.parseInt(state2.toString(), 2)), 2);
        return data1 + data2;
    }
}
